using Quartz;
using Share;
using UsingDataDemo.Jobs;
using Serilog;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddSerilog();

builder.Services.AddQuartz(q =>
{
    q.AddJob<SampleJob>(SampleJob.Key);

    q.AddTrigger(t =>
    {
        t.WithIdentity("trigger", "group1")
            .ForJob(SampleJob.Key)
            .UsingJobData("Greeting", "Hi")
            .UsingJobData("User", "Fan")
            .WithSimpleSchedule(s => s.WithIntervalInSeconds(5));
    });

    q.AddJob<CounterJob>(CounterJob.Key,j=>j.StoreDurably());
    q.AddTrigger(t =>
    {
        t.WithIdentity("Counter trigger", "data-passing")
        //.UsingJobData("CurrentCounter", 100)
        .ForJob(CounterJob.Key)
        .WithSimpleSchedule(s => s.WithIntervalInSeconds(10).RepeatForever());
    });

    q.AddJob<ShowCounterJob>(ShowCounterJob.Key);
    q.AddTrigger(t =>
    {
        t.WithIdentity("ShowCounter trigger", "data-passing")
        .ForJob(ShowCounterJob.Key)
        .WithSimpleSchedule(s => s.WithIntervalInSeconds(10).RepeatForever())
        .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second));
    });

    //q.AddJobListener<SampleJobListener>(GroupMatcher<JobKey>.GroupEquals("group"));
});
//builder.Services.AddQuartzHostedService();
builder.Services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);

var app = builder.Build();

//var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
//var scheduler = await schedulerFactory.GetScheduler();
//await scheduler.ScheduleJob()
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
